import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_devices/icons/src/antd_icons.dart';
import 'package:smart_devices/ui/draw_under_line.dart';
import 'package:smart_devices/ui/max_line_wrap.dart';
import 'package:smart_devices/ui/scroll_refresh_config.dart';
import 'package:smart_devices/utils/colors.dart';
import 'package:smart_devices/utils/styles.dart';
import 'package:smart_devices/utils/tools.dart';

import 'record_logic.dart';

class InspectionRecordView extends GetView<InspectionRecordLogic> {
  const InspectionRecordView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back_ios,
            color: Colors.white,
            size: 15,
          ),
          onPressed: Get.back,
        ),
        automaticallyImplyLeading: false,
        title: Text(
          '巡检记录',
          style: StylesUtils.customTextStyle(
              fontSize: 18, fontWeight: FontWeight.w700),
        ),
        centerTitle: true,
        bottom: PreferredSize(
            preferredSize: const Size.fromHeight(50),
            child: Container(
              height: 50,
              padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 7),
              decoration: const BoxDecoration(color: Colors.white),
              child: createRow(),
            )),
        elevation: 0,
      ),
      body: Obx(() => scrollRefreshConfig(context)),
    );
  }

  Row createRow() {
    return Row(
      children: [
        Expanded(
            child: Container(
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(5),
              border: Border.all(
                  color: ColorsUtils.parseColorFromHexString(
                      ColorsUtils.colorE4e4))),
          child: Row(
            children: [
              Container(
                  width: 36,
                  alignment: Alignment.center,
                  child: Icon(
                    AntdIcons.search,
                    size: 20,
                    color: ColorsUtils.parseColorFromHexString(
                        ColorsUtils.color9999),
                  )),
              Expanded(
                  child: Obx(() => TextField(
                        textDirection: TextDirection.ltr,
                        controller: TextEditingController(
                            text: controller.search.value),
                        cursorColor: ColorsUtils.parseColorFromHexString(
                            ColorsUtils.color1c24),
                        style: StylesUtils.customTextStyle(
                            fontSize: 16, color: ColorsUtils.color1c24),
                        textAlign: TextAlign.left,
                        textAlignVertical: TextAlignVertical.center,
                        cursorWidth: 2,
                        // cursorHeight: 18,
                        cursorRadius: const Radius.circular(2),
                        decoration: InputDecoration(
                            hintText: '请输入报修主题/报修单号/设备名称',
                            hintStyle: StylesUtils.customTextStyle(
                                color: ColorsUtils.colorCccc, fontSize: 13),
                            isDense: true,
                            isCollapsed: true,
                            border: InputBorder.none,
                            focusColor: ColorsUtils.parseColorFromHexString(
                                ColorsUtils.color1c24),
                            contentPadding:
                                const EdgeInsets.symmetric(vertical: 13),
                            focusedBorder: InputBorder.none),
                        onChanged: (String text) {
                          controller.search.value = text;
                        },
                      ))),
            ],
          ),
        )),
        Container(
          width: 60,
          alignment: Alignment.center,
          child: Text.rich(TextSpan(children: [
            WidgetSpan(
                alignment: PlaceholderAlignment.middle,
                child: ToolsUtils.assetImageSvg('u2226.svg',
                    width: 15, height: 15)),
            TextSpan(
                text: '筛选',
                style:
                    StylesUtils.customTextStyle(color: ColorsUtils.color3333))
          ])),
        ),
      ],
    );
  }

  Widget scrollRefreshConfig(BuildContext context) {
    return ScrollRefreshConfig(
      refreshController: controller.refreshController,
      onRefresh: controller.onRefresh,
      onLoading: controller.onLoading,
      child: listViewBuilder(context),
    );
  }

  Widget listViewBuilder(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext c, int index) {
        return DrawUnderLine(
            child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 10),
          margin: index == 0 ? const EdgeInsets.only(top: 10) : EdgeInsets.zero,
          decoration: const BoxDecoration(color: Colors.white),
          child: Column(
            children: [
              Container(
                height: 40,
                alignment: Alignment.centerLeft,
                child: Text(
                  controller.pageList[index].title,
                  style: StylesUtils.customTextStyle(
                      color: ColorsUtils.color0052,
                      fontSize: 16,
                      fontWeight: FontWeight.w700),
                ),
              ),
              Container(
                height: 30,
                alignment: Alignment.centerLeft,
                child: MaxLineWrap(
                  maxLine: 1,
                  itemAlignment: MaxLineWrapAlignment.start,
                  children: [
                    Text(
                      '任务编号',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: 13),
                    ),
                    Text(
                      controller.pageList[index].taskNo,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.colorAaaa, fontSize: 13),
                    )
                  ],
                ),
              ),
              Container(
                height: 30,
                alignment: Alignment.centerLeft,
                child: MaxLineWrap(
                  maxLine: 1,
                  itemAlignment: MaxLineWrapAlignment.start,
                  children: [
                    Text(
                      '设备名称',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: 13),
                    ),
                    Text(
                      controller.pageList[index].name,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.colorAaaa, fontSize: 13),
                    )
                  ],
                ),
              ),
              Container(
                height: 30,
                alignment: Alignment.centerLeft,
                child: MaxLineWrap(
                  maxLine: 1,
                  itemAlignment: MaxLineWrapAlignment.start,
                  children: [
                    Text(
                      '设备编号',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: 13),
                    ),
                    Text(
                      controller.pageList[index].code,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.colorAaaa, fontSize: 13),
                    )
                  ],
                ),
              ),
              Container(
                height: 30,
                alignment: Alignment.centerLeft,
                child: MaxLineWrap(
                  maxLine: 1,
                  itemAlignment: MaxLineWrapAlignment.start,
                  children: [
                    Text(
                      '存放位置',
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.color3333, fontSize: 13),
                    ),
                    Text(
                      controller.pageList[index].location,
                      overflow: TextOverflow.ellipsis,
                      maxLines: 1,
                      style: StylesUtils.customTextStyle(
                          color: ColorsUtils.colorAaaa, fontSize: 13),
                    )
                  ],
                ),
              ),
            ],
          ),
        ));
      },
      itemCount: controller.pageList.length,
      itemExtent: 170,
    );
  }
}
